﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Media;

namespace Relay.UI.Controls.Docking.Primitives {
    internal class DockHintAdorner : Adorner {
        #region Fields
        private Rectangle _rect = new Rectangle();
        private DockOverlayHitTestResult _hitTest = DockOverlayHitTestResult.None;
        private FrameworkElement _element = null;
        private double _size = 0;
        #endregion

        #region Constructors
        public DockHintAdorner(FrameworkElement element, DockOverlayHitTestResult hitTest, double hintSize) : base(element) {
            SnapsToDevicePixels = true;
            _rect.Fill = new SolidColorBrush(SystemColors.HighlightColor) {
                Opacity = 0.5
            };

            AddVisualChild(_rect);

            _size = hintSize;
            _element = element;
            _hitTest = hitTest;
        }
        #endregion

        #region Overrides
        protected override Visual GetVisualChild(int index) {
            if (index != 0) throw new ArgumentOutOfRangeException("index");
            return _rect;
        }

        protected override int VisualChildrenCount {
            get {
                return 1;
            }
        }

        protected override Size ArrangeOverride(Size finalSize) {
            Rect finalRect = new Rect();

            switch (_hitTest) {
                case DockOverlayHitTestResult.Bottom:
                case DockOverlayHitTestResult.Top:
                    finalRect.Width = finalSize.Width;
                    finalRect.Height = _size;

                    if (_hitTest == DockOverlayHitTestResult.Bottom)
                        finalRect.Y = _element.ActualHeight - finalRect.Height;

                    break;
                case DockOverlayHitTestResult.Left:
                case DockOverlayHitTestResult.Right:
                    finalRect.Height = finalSize.Height;
                    finalRect.Width = _size;

                    if (_hitTest == DockOverlayHitTestResult.Right)
                        finalRect.X = _element.ActualWidth - finalRect.Width;

                    break;
                case DockOverlayHitTestResult.Center:
                    finalRect = new Rect(0, 0, finalSize.Width, finalSize.Height);
                    break;
            }
            _rect.Arrange(finalRect);
            return finalSize;
        }
        #endregion
    }
}
